package com.ubst.smartad.impl;

import java.sql.*;
import java.util.*;
import java.io.*;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import com.ubst.smartad.dao.TSASystemPolicyDO;
import com.ubst.smartad.common.TimeUtil;
import com.ubst.smartad.logger.Debug;

public class TSASystemPolicyMngImpl {
	
	Connection con = null;
	private String serverName;
	private Debug debug;

	/** 
     * 생성자
     *   
     *   
    */ 
	public TSASystemPolicyMngImpl(Connection con, String serverName, String log_level) {
		this.con = con;
		this.serverName = serverName;
		this.debug = new Debug("TSASystemPolicyMngImpl", log_level);

	}
	
    /** 
	 * @author KJH
     * 시스템 정책 등록
     *   
     */  
    public void insertSystemPolicy(TSASystemPolicyDO systemPolicyDO) throws SQLException{
        PreparedStatement pstmt=null;

        StringBuffer queryBuf = null;
        try {

			long l_cur_date = TimeUtil.getCurrentTime();

            queryBuf = new StringBuffer();
			queryBuf.append("INSERT INTO TSA_SYSTEM_POLICY " )
				.append(" ( ")
				.append("  SYSTEMID ,SYSTYPE ,ROOTLOGIN ,RETRYBLOCK ,RETRYSUSPEND ")
				.append(" ,UNUSEDACCOUNT ,SESSIONCOUNT ,PASSMINLFLAG ,PASSMAXLFLAG ,PASSHISFLAG ")
				.append(" ,PASSUSERNAME ,PASSCOMPLEXITY ,CHANGEPASS ,LOGOFFHOUR ,ENABLEADMIN ")
				.append(" ,ENABLEGUEST ,PASSDIFFFLAG ,PASSSEQFLAG ,PASSDICFLAG ,OLDPASSFLAG ")
				.append(" ,CRESULT ,NSTATUS ,LASTSYNC ")
				.append(" ) ")
				.append(" VALUES")
				.append(" ( ") 
				.append("  ? ,? ,? ,? ,? ")
				.append(" ,? ,? ,? ,? ,? ")
				.append(" ,? ,? ,? ,? ,? ")
				.append(" ,? ,? ,? ,? ,? ")
				.append(" ,? ,? ,? ")
				.append(" ) ");

			debug.debug("insertSystemPolicy ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setString(idx++, systemPolicyDO.systemid);
			pstmt.setString(idx++, systemPolicyDO.systype);
			pstmt.setString(idx++, systemPolicyDO.rootlogin);
			pstmt.setString(idx++, systemPolicyDO.retryblock);
			pstmt.setString(idx++, systemPolicyDO.retrysuspend);
			pstmt.setString(idx++, systemPolicyDO.unusedaccount);
			pstmt.setString(idx++, systemPolicyDO.sessioncount);
			pstmt.setString(idx++, systemPolicyDO.passminlflag);
			pstmt.setString(idx++, systemPolicyDO.passmaxlflag);
			pstmt.setString(idx++, systemPolicyDO.passhisflag);
			pstmt.setString(idx++, systemPolicyDO.passusername);
			pstmt.setString(idx++, systemPolicyDO.passcomplexity);
			pstmt.setString(idx++, systemPolicyDO.changepass);
			pstmt.setString(idx++, systemPolicyDO.logoffhour);
			pstmt.setString(idx++, systemPolicyDO.enableadmin);
			pstmt.setString(idx++, systemPolicyDO.enableguest);
			pstmt.setString(idx++, systemPolicyDO.passdiffflag);
			pstmt.setString(idx++, systemPolicyDO.passseqflag);
			pstmt.setString(idx++, systemPolicyDO.passdicflag);
			pstmt.setString(idx++, systemPolicyDO.oldpassflag);
			pstmt.setString(idx++, systemPolicyDO.cresult);
			pstmt.setInt(idx++, systemPolicyDO.nstatus);
			pstmt.setString(idx++, TimeUtil.getDateString(l_cur_date, "yyyyMMddHHmmss"));

            pstmt.execute();
            pstmt.close();
			queryBuf = null;

        }catch (SQLException e){
			throw e;
        }finally{
            if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
        }

    }

    /** 
	 * @author KJH
     * 시스템 정책 변경
     *   
     */  
    public void updateSystemPolicy(TSASystemPolicyDO systemPolicyDO) throws SQLException{
        PreparedStatement pstmt=null;

        StringBuffer queryBuf = null;
        try {

			long l_cur_date = TimeUtil.getCurrentTime();

            queryBuf = new StringBuffer();
			queryBuf.append("UPDATE TSA_SYSTEM_POLICY " )
				.append(" SET ")
				.append("  SYSTYPE=? ,ROOTLOGIN=? ,RETRYBLOCK=? ,RETRYSUSPEND=? ,UNUSEDACCOUNT=? ")
				.append(" ,SESSIONCOUNT=? ,PASSMINLFLAG=? ,PASSMAXLFLAG=? ,PASSHISFLAG=? ,PASSUSERNAME=? ")
				.append(" ,PASSCOMPLEXITY=? ,CHANGEPASS=? ,LOGOFFHOUR=? ,ENABLEADMIN=? ,ENABLEGUEST=? ")
				.append(" ,PASSDIFFFLAG=? ,PASSSEQFLAG=? ,PASSDICFLAG=? ,OLDPASSFLAG=? ,CRESULT=? ")
				.append(" ,NSTATUS=? ,LASTSYNC=? ")
				.append(" WHERE SYSTEMID=? ");

			debug.debug("updateSystemPolicy ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setString(idx++, systemPolicyDO.systype);
			pstmt.setString(idx++, systemPolicyDO.rootlogin);
			pstmt.setString(idx++, systemPolicyDO.retryblock);
			pstmt.setString(idx++, systemPolicyDO.retrysuspend);
			pstmt.setString(idx++, systemPolicyDO.unusedaccount);
			pstmt.setString(idx++, systemPolicyDO.sessioncount);
			pstmt.setString(idx++, systemPolicyDO.passminlflag);
			pstmt.setString(idx++, systemPolicyDO.passmaxlflag);
			pstmt.setString(idx++, systemPolicyDO.passhisflag);
			pstmt.setString(idx++, systemPolicyDO.passusername);
			pstmt.setString(idx++, systemPolicyDO.passcomplexity);
			pstmt.setString(idx++, systemPolicyDO.changepass);
			pstmt.setString(idx++, systemPolicyDO.logoffhour);
			pstmt.setString(idx++, systemPolicyDO.enableadmin);
			pstmt.setString(idx++, systemPolicyDO.enableguest);
			pstmt.setString(idx++, systemPolicyDO.passdiffflag);
			pstmt.setString(idx++, systemPolicyDO.passseqflag);
			pstmt.setString(idx++, systemPolicyDO.passdicflag);
			pstmt.setString(idx++, systemPolicyDO.oldpassflag);
			pstmt.setString(idx++, systemPolicyDO.cresult);
			pstmt.setInt(idx++, systemPolicyDO.nstatus);
			pstmt.setString(idx++, TimeUtil.getDateString(l_cur_date, "yyyyMMddHHmmss"));
			pstmt.setString(idx++, systemPolicyDO.systemid);

            pstmt.execute();
            pstmt.close();
			queryBuf = null;

        }catch (SQLException e){
			throw e;
        }finally{
            if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
        }

    }

	/**
	 * @author : KJH 
	 * 시스템 정책 출력
	 *
	 */
	public TSASystemPolicyDO selectSystemPolicy(String systemid) throws SQLException {

		PreparedStatement pstmt=null;
		ResultSet rs = null;

		TSASystemPolicyDO systemPolicyDO = null;

		try {
			
			StringBuffer queryBuf = new StringBuffer();
			queryBuf.append("SELECT ")
				.append("  SYSTYPE ,ROOTLOGIN ,RETRYBLOCK ,RETRYSUSPEND ,UNUSEDACCOUNT ")
				.append(" ,SESSIONCOUNT ,PASSMINLFLAG ,PASSMAXLFLAG ,PASSHISFLAG ,PASSUSERNAME ")
				.append(" ,PASSCOMPLEXITY ,CHANGEPASS ,LOGOFFHOUR ,ENABLEADMIN ,ENABLEGUEST ")
				.append(" ,PASSDIFFFLAG ,PASSSEQFLAG ,PASSDICFLAG ,OLDPASSFLAG ,CRESULT ")
				.append(" ,NSTATUS ,LASTSYNC ")
				.append(" FROM TSA_SYSTEM_POLICY ")
				.append(" WHERE SYSTEMID=? ");

			debug.debug("selectSystemPolicy ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setString(idx++, systemid);

			rs = pstmt.executeQuery();

			if ( rs.next() ){
				systemPolicyDO = new TSASystemPolicyDO();
				systemPolicyDO.systemid = systemid;
				systemPolicyDO.systype = rs.getString("SYSTYPE");
				systemPolicyDO.rootlogin = rs.getString("ROOTLOGIN");
				systemPolicyDO.retryblock = rs.getString("RETRYBLOCK");
				systemPolicyDO.retrysuspend = rs.getString("RETRYSUSPEND");
				systemPolicyDO.unusedaccount = rs.getString("UNUSEDACCOUNT");
				systemPolicyDO.sessioncount = rs.getString("SESSIONCOUNT");
				systemPolicyDO.passminlflag = rs.getString("PASSMINLFLAG");
				systemPolicyDO.passmaxlflag = rs.getString("PASSMAXLFLAG");
				systemPolicyDO.passhisflag = rs.getString("PASSHISFLAG");
				systemPolicyDO.passusername = rs.getString("PASSUSERNAME");
				systemPolicyDO.passcomplexity = rs.getString("PASSCOMPLEXITY");
				systemPolicyDO.changepass = rs.getString("CHANGEPASS");
				systemPolicyDO.logoffhour = rs.getString("LOGOFFHOUR");
				systemPolicyDO.enableadmin = rs.getString("ENABLEADMIN");
				systemPolicyDO.enableguest = rs.getString("ENABLEGUEST");
				systemPolicyDO.passdiffflag = rs.getString("PASSDIFFFLAG");
				systemPolicyDO.passseqflag = rs.getString("PASSSEQFLAG");
				systemPolicyDO.passdicflag = rs.getString("PASSDICFLAG");
				systemPolicyDO.oldpassflag = rs.getString("OLDPASSFLAG");
				systemPolicyDO.cresult = rs.getString("CRESULT");
				systemPolicyDO.nstatus = rs.getInt("NSTATUS");
				systemPolicyDO.lastsync = rs.getString("LASTSYNC");
			}

			rs.close();
			pstmt.close();

		}catch (SQLException e){
			throw e;
		}finally{
			if ( rs != null) try{rs.close();}catch(SQLException ee){}
			if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
		}

		return systemPolicyDO;
	}

    /** 
	 * @author KJH
     * 시스템 정책 삭제
     *   
     */  
    public void deleteSystemPolicy(String systemid) throws SQLException{
        PreparedStatement pstmt=null;

        StringBuffer queryBuf = null;
        try {

            queryBuf = new StringBuffer();
			queryBuf.append("DELETE FROM TSA_SYSTEM_POLICY " )
				.append(" WHERE SYSTEMID=? ");

			debug.debug("deleteSystemPolicy ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setString(idx++, systemid);

            pstmt.execute();
            pstmt.close();
			queryBuf = null;

        }catch (SQLException e){
			throw e;
        }finally{
            if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
        }

    }

}
